package com.wutao.panzh.index.content.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.panzh.UtilEntity.ResultVO;
import com.panzh.entity.DispalySku;
import com.panzh.entity.HotelItemSpu;
import com.panzh.entity.HotelSaleAttr;
import com.panzh.entity.index.UserInfo;
import com.panzh.myconst.CommonConst;
import com.panzh.myconst.UserContst;
import com.wutao.panzh.service.HotelSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexHandler {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private ObjectMapper mapper;

    @Reference
    private HotelSkuService hotelSkuService;

    @Autowired
    private IndexHotelHandler indexHotelHandler;

    //获得userInfo的json对象；
    private void getJsonUserInfo() {
        try {
            HttpSession session = request.getSession();
//        System.out.println(UserIndexHandler.class.getName()+"类前置代码执行！");
            UserInfo userInfo = (UserInfo) session.getAttribute(UserContst.USER_INFO_IS_EXIST);
            //防止密码泄露；
            if (userInfo != null){
                userInfo.setPasswd("");
            }
            String userInfoJson = mapper.writeValueAsString(userInfo);
            request.setAttribute(UserContst.USER_INFO,userInfoJson);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }


    /**
     * 第一次进入前端的页面，还是有些小激动呀，天呐，我这两个月就弄了这个破玩意，真是~~浪费感情。
     *
     * @return 首页的thymeleaf的页面
     */
    @RequestMapping("/index")
    public String getIndex() {
        getJsonUserInfo();
        return "index";
    }

    //    <li><a href="information.html">热门资讯</a></li>
    @RequestMapping("/information")
    public String getInformation() {
        getJsonUserInfo();
        return "information";
    }

    //    <li><a href="hotSpots.html">热门景点</a></li>
//    window.location.href="http://localhost:8184/hotSpots.html?classficationId="+classificationId+"";
    @RequestMapping("/hotSpots")
    public String getHotSpots(@RequestParam(value = "classficationId",required = false,defaultValue = "0") String classficationId,
                              Model model) {
        getJsonUserInfo();
        model.addAttribute("classificationId",classficationId);
        return "hotSpots";
    }

    //    <li><a href="specialFood.html">特色美食</a></li>
    @RequestMapping("/specialFood")
    public String getSpecialFood(@RequestParam(value = "classficationId",required = false,defaultValue = "0") String classficationId,
                                 Model model) {
        getJsonUserInfo();
        model.addAttribute("classificationId",classficationId);
        return "specialFood";
    }

    //    <li><a href="touristRoute.html">旅游路线</a></li>
    @RequestMapping("/touristRoute")
    public String getTouristRoute(@RequestParam(value = "classficationId",required = false,defaultValue = "0") String classficationId,
                                  Model model) {
        getJsonUserInfo();
        model.addAttribute("classificationId",classficationId);
        return "touristRoute";
    }

    //    <li><a href="hotelReservation.html">酒店预订</a></li>
    @RequestMapping("/hotelReservation")
    public String getHotelReservation(@RequestParam(value = "classficationId",required = false,defaultValue = "0") String classficationId,
                                      Model model) {
        getJsonUserInfo();
        getHotelInfo(model);
        model.addAttribute("classificationId",classficationId);
        return "hotelReservation";
    }


    //    toLogin
    @RequestMapping("/toLogin")
    public String getLogin(){
        getJsonUserInfo();
        return "login";
    }

    //获得酒店的信息，包括分类信息等等;
    private void getHotelInfo(Model model) {
        List<HotelSaleAttr> hotelSaleAttrs = indexHotelHandler.getClassification();
        List<HotelItemSpu> hotelItemSpus = indexHotelHandler.getHotelSpu();
        model.addAttribute("hotelSaleAttrs",hotelSaleAttrs);
        model.addAttribute("hotelItemSpus",hotelItemSpus);
    }

//    @RequestMapping("get/sku/by/spuId")
//    @ResponseBody
//    public ResultVO getSkuBySpuId(@RequestParam(value = "spuId") String spuId){
//        try {
//            List<DispalySku> skuPage = hotelSkuService.getDispalyHotelSkuPageBySpuId(spuId);
//            return new ResultVO(ResultVO.SUCCESS,ResultVO.NO_MSG,skuPage);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new ResultVO(ResultVO.FAILED,ResultVO.NO_MSG,ResultVO.NO_DATA);
//        }
//    }
//
//    @RequestMapping("add/book/hotel")
//    @ResponseBody
//    public ResultVO addBookHotel(@RequestParam(value = "skuId",required = true) String skuId,
//                                 @RequestParam(value = "userId",required = true) String userId){
//        try {
//            hotelSkuService.addBookHotel(skuId,userId);
//            return new ResultVO<String>(ResultVO.SUCCESS, CommonConst.INFORMATION_SAVED_SUCCESSFULLY, ResultVO.NO_MSG);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new ResultVO<String>(ResultVO.FAILED, CommonConst.DATA_SAVING_FAILED, ResultVO.NO_MSG);
//        }
//    }
}
